package com.scenery;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.scenery.services.SetManager;
import com.scenery.services.UserServices;
import com.scenery.util.HttpUtil;
import com.scenery.vo.Scenery;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/***
 * 点评
 * **/
public class DianPingActivity extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	EditText userId,sceneryId,dianPingText;
	Button btnDianping;
	String tag=DianPingActivity.class.getName();
	String url=Constans.dianping_URL;
	Scenery bean;
	SetManager setm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		setContentView(R.layout.dianping);
		setm=new SetManager(DianPingActivity.this);
		Bundle bundle = this.getIntent().getExtras();
		bean=	bundle.getParcelable("bean");
		
		userId=(EditText) findViewById(R.id.userId);
		sceneryId=(EditText) findViewById(R.id.sceneryId);
		
		dianPingText=(EditText) findViewById(R.id.dianPingText);
		btnDianping=(Button) findViewById(R.id.btnDianping);
		sceneryId.setText(bean.getSceneryName());
		userId.setText(setm.getUserId());
		btnDianping.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				// TODO Auto-generated method stub
				Log.d(tag, "btnDianping");
				if(!HttpUtil.isNetWorkConnected(DianPingActivity.this))
				{
					Toast.makeText(DianPingActivity.this, "网络不通！", Toast.LENGTH_LONG).show();
				}else {
					DianPingAsyncTask dianping=new DianPingAsyncTask();
					
					dianping.init(userId.getText().toString(), bean.getSceneryId(), dianPingText.getText().toString());
					dianping.execute(0);
				}
			
			
			}
		});
	}
	class DianPingAsyncTask extends AsyncTask<Integer,Integer,Integer>{
		String userId="",sceneryId="",dianPingText;
		void init(String userId,String sceneryId,String dianPingText)
		{
			this.userId=userId;
			this.sceneryId=sceneryId;
			this.dianPingText=dianPingText;
		}
		@Override
		protected Integer doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();
			parameters.add(new BasicNameValuePair("action","add"));
			parameters.add(new BasicNameValuePair("userId",userId));
			parameters.add(new BasicNameValuePair("sceneryId",sceneryId));
			parameters.add(new BasicNameValuePair("text",dianPingText));
			String res=HttpUtil.httpPost(parameters, "http://"+setm.getIp()+":"+setm.getWebServerport()+Constans.dianping_URL);
			Log.d(tag, "doInBackground:"+res);
			return 0;
		}
		@Override
		protected void onPostExecute(Integer result) 
		{
			Log.d(tag, "onPostExecute:"+result);
			switch(result)
			{
				case 0:
					Toast.makeText(DianPingActivity.this, "成功", Toast.LENGTH_LONG).show();
					break;
				case 1:
					Toast.makeText(DianPingActivity.this, "连接不到服务器", Toast.LENGTH_LONG).show();
					break;
				case 2:
					Toast.makeText(DianPingActivity.this, "异常", Toast.LENGTH_LONG).show();
					break;
			}
		}
		
	};
}
